package com.haochang.chunkvod.tools.vod.other;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.haochang.base.BaseTextView;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.model.BaseInfo;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.support.v4.graphics.ColorUtils;
import android.text.TextUtils;
import android.view.View;

/**
 *@author qintao
 *created at 2016/11/4 17:54
 * Drawable工具类
 */
public class DrawableUtils
{

	/**
	 * RGB颜色值的正则表达式
	 */
	private static final String RGB_COLOR_REGEX = "^#[0-9a-fA-F]{6}";

	/**
	 *
	 * @param context context
	 * @param backGroundColor 背景色
	 * @param borderColor 边框色
	 * @param isSetAlpha 是否透明
	 * @return gradientDrawable
	 */
	public static GradientDrawable getDrawable(Context context, int backGroundColor, int borderColor, boolean isSetAlpha)
	{
		GradientDrawable gradientDrawable = new GradientDrawable();
		if (isSetAlpha)
			gradientDrawable.setColor(rgbToArgb(backGroundColor, 127));//设置透明颜色
		else
			gradientDrawable.setColor(backGroundColor);//设置颜色
		if (backGroundColor != borderColor)
			gradientDrawable.setStroke(DipPxConversion.dip2px(context, 3), borderColor);//描边
		gradientDrawable.setGradientType(GradientDrawable.RECTANGLE);//设置显示的样式
		gradientDrawable.setCornerRadius(4);//设置圆角的半
		//		if (isSetAlpha)
		//			gradientDrawable.setAlpha(127);//设置点击效果半透明，在p2000设置无效
		return gradientDrawable;
	}

	/**
	 *获取selector
	 * @param context context
	 * @param baseInfo baseInfo
	 * @return
	 */
	public static StateListDrawable getSelector(Context context, BaseInfo baseInfo)
	{
		StateListDrawable stateListDrawable = new StateListDrawable();
		try
		{
			Drawable pressDrawable = getDrawable(context, Color.parseColor(baseInfo.getRoomBgColor()), Color.parseColor(baseInfo.getRoomBorderColor()), true);
			Drawable normalDrawable = getDrawable(context, Color.parseColor(baseInfo.getRoomBgColor()), Color.parseColor(baseInfo.getRoomBorderColor()), false);
			//给当前的颜色选择器添加选中图片指向状态，未选中图片指向状态
			stateListDrawable.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_pressed }, pressDrawable);
			stateListDrawable.addState(new int[] { android.R.attr.state_enabled }, normalDrawable);
			stateListDrawable.addState(new int[] {}, normalDrawable);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return stateListDrawable;
	}

	/**
	 *获取selector
	 * @param context context
	 * @param baseInfo baseInfo
	 * @return
	 */
	public static StateListDrawable getSelectorNoPressed(Context context, BaseInfo baseInfo)
	{
		StateListDrawable stateListDrawable = new StateListDrawable();
		try
		{
			Drawable normalDrawable = getDrawable(context, Color.parseColor(baseInfo.getRoomBgColor()), Color.parseColor(baseInfo.getRoomBorderColor()), false);
			//给当前的颜色选择器添加选中图片指向状态，未选中图片指向状态
			stateListDrawable.addState(new int[] {}, normalDrawable);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return stateListDrawable;
	}

	/**
	 * 透明度50% 对应80
	 * @param color
	 * @return
	 */
	public static String getAlphaColorLocal(String color)
	{
		if (!TextUtils.isEmpty(color))
		{
			Pattern pattern = Pattern.compile(RGB_COLOR_REGEX);
			Matcher matcher = pattern.matcher(color);
			if (matcher.matches())
			{
				System.out.println(color.substring(0, 1) + "80" + color.substring(1));
				return "#" + "80" + color.substring(1);
			}
		}
		return "#000000";
	}

	/**
	 * 颜色rgb转换为ragb
	 * @param color
	 * @param alpha
	 * @return
	 */
	private static int rgbToArgb(int color, int alpha)
	{
		if (alpha < 0 || alpha > 255)
		{
			throw new IllegalArgumentException("alpha must be between 0 and 255.");
		}
		int red = Color.red(color);
		int green = Color.green(color);
		int blue = Color.blue(color);
		return Color.argb(alpha, red, green, blue);
	}

	/**
	 * 获取颜色透明度
	 * @param color
	 * @return
	 */
	public static int getAlphaColor(String color)
	{
		return ColorUtils.setAlphaComponent(Color.parseColor(color), 127);
	}

	/**
	 * 设置服务器下放的房间颜色
	 *
	 * @param status
	 * @param usage
	 * @param view
	 * @param tv_roomStatus
	 */
	public static void setRoomStyleByService(Context mContext, String status, String usage, View view, BaseTextView tv_roomStatus, List<BaseInfo> baseInfoList)
	{
		if (TextUtils.isEmpty(status) || view == null || tv_roomStatus == null || CollectionUtils.isEmpty(baseInfoList))
			return;
		for (int i = 0; i < baseInfoList.size(); i++)
		{
			BaseInfo baseInfo = baseInfoList.get(i);
			if (TextUtils.equals(baseInfo.getRoomStatus(), status) && TextUtils.equals(baseInfo.getRoomUsage(), usage))
			{
				view.setBackgroundDrawable(DrawableUtils.getSelector(mContext, baseInfo));
				tv_roomStatus.setText(baseInfo.getRoomTag());
				break;
			}
		}
	}

	/**
	 * 设置服务器下放的房间颜色
	 *
	 * @param status
	 * @param usage
	 * @param view
	 * @param tv_roomStatus
	 */
	public static void setRoomStyleByServiceNoClick(Context mContext, String status, String usage, View view, BaseTextView tv_roomStatus,
			List<BaseInfo> baseInfoList)
	{
		if (TextUtils.isEmpty(status) || view == null || tv_roomStatus == null || CollectionUtils.isEmpty(baseInfoList))
			return;
		for (int i = 0; i < baseInfoList.size(); i++)
		{
			BaseInfo baseInfo = baseInfoList.get(i);
			if (TextUtils.equals(baseInfo.getRoomStatus(), status) && TextUtils.equals(baseInfo.getRoomUsage(), usage))
			{
				view.setBackgroundDrawable(DrawableUtils.getSelectorNoPressed(mContext, baseInfo));
				tv_roomStatus.setText(baseInfo.getRoomTag());
				break;
			}
		}
	}

	/**
	 * 设置服务器下放的桌台颜色
	 *
	 * @param status
	 * @param view
	 * @param tv_roomStatus
	 */
	public static void setDeskStyleByService(Context mContext, String status, View view, BaseTextView tv_roomStatus, List<BaseInfo> baseInfoList)
	{
		if (TextUtils.isEmpty(status) || view == null || tv_roomStatus == null || CollectionUtils.isEmpty(baseInfoList))
			return;
		for (int i = 0; i < baseInfoList.size(); i++)
		{
			BaseInfo baseInfo = baseInfoList.get(i);
			if (TextUtils.equals(baseInfo.getRoomStatus(), status))
			{
				view.setBackgroundDrawable(DrawableUtils.getSelector(mContext, baseInfo));
				tv_roomStatus.setText(baseInfo.getRoomTag());
				break;
			}
		}
	}

	/**
	 * 根据房间状态设置房间默认显示背景
	 *
	 * @param status        状态
	 * @param usage         用途
	 * @param view          设置背景view
	 * @param tv_roomStatus 房间状态view
	 */
	public static void setDefaultRoomStyle(Context mContext, String status, String usage, View view, BaseTextView tv_roomStatus)
	{
		if (TextUtils.isEmpty(status) || view == null || tv_roomStatus == null)
			return;
		if (status.equals("guiding") && usage.equals("sing"))
		{
			view.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_room_gray_orange));
			tv_roomStatus.setText("带");
		} else if (status.equals("using") && usage.equals("sing"))
		{
			view.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_room_orange_orange));
			tv_roomStatus.setText("用");
		} else if (status.equals("using") && usage.equals("dining"))
		{
			view.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_room_orange_orange));
			tv_roomStatus.setText("餐");
		} else if (status.equals("using") && usage.equals("meeting"))
		{
			view.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_room_orange_blue));
			tv_roomStatus.setText("会");
		} else if (status.equals("using") && usage.equals("debug"))
		{
			view.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_room_orange_blue));
			tv_roomStatus.setText("试");
		} else if (status.equals("paying") && usage.equals("sing"))
		{
			view.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_room_orange_orange));
			tv_roomStatus.setText("买");
		} else if (status.equals("prepay") && usage.equals("sing"))
		{
			view.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_room_blue_orange));
			tv_roomStatus.setText("预买");
		} else if (status.equals("paied") && usage.equals("sing"))
		{
			view.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_room_blue_orange));
			tv_roomStatus.setText("未离");
		}
	}

	/**
	 * 根据桌台状态设置桌台默认显示背景
	 *
	 * @param status        状态
	 * @param view          设置背景view
	 * @param tv_roomStatus 房间状态view
	 */
	public static void settDefaultDeskStyle(Context mContext, String status, View view, BaseTextView tv_roomStatus)
	{
		if (TextUtils.isEmpty(status) || view == null || tv_roomStatus == null)
			return;
		if (status.equals("empty"))
		{
			view.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_room_gray_gray));
			tv_roomStatus.setText("空");
		} else if (status.equals("using"))
		{
			view.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_room_orange_orange));
			tv_roomStatus.setText("用");
		} else if (status.equals("paied"))
		{
			view.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_room_blue_blue));
			tv_roomStatus.setText("未离");
		} else if (status.equals("paying"))
		{
			view.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_room_blue_orange));
			tv_roomStatus.setText("买");
		}
	}

}
